﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.Reports;
namespace ERP.Hoadon
{
    public partial class viewERP_Giaoviec : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
        public cs_erp_khachhang_sodoInfo hh = new cs_erp_khachhang_sodoInfo();

        public cs_erp_hanghoaInfo hanghoa = new cs_erp_hanghoaInfo();

        int count = 0;
        public int flag = 1;
        public viewERP_Giaoviec()
        {
            InitializeComponent();
        }

        private void viewERP_Giaoviec_Load(object sender, EventArgs e)
        {
            this.Load_Data();
            this.Load_Info();
            this.Load_DV();
            if (this.hoadon.nguoinhanviec != 0)
            {
                //this.cboDonvi.EditValue = this.hoadon.
                this.txtNgayhoanthanh.EditValue = this.hoadon.ngayhoanthanh;
                this.txtGhichu.Text = this.hoadon.ghichu;
            }
            this.CheckUser();
        }
        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
        private void Load_Info()
        {
            this.txtHoten.Text = this.kh.tenkhachhang;
            this.txtDiachi.Text = this.kh.diachi;
            this.txtDienthoai.Text = this.kh.dienthoai;
            this.txtHoadon.Text = this.hoadon.sohd;
            this.txtNgayban.EditValue = this.hoadon.ngayban;
            this.txtNgaythu.EditValue = this.hoadon.ngayhentra;
            this.txtNgayhoanthanh.EditValue = this.hoadon.ngayhentra.AddDays(-2);
        }

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            //this.hanghoa.Get_List = this.kh.id_kh;
            ds = this.hanghoa.Get_List_GV(this.hoadon.id_hd);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void Load_DV()
        {
            DataSet ds = new DataSet("ds");
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            ds = dv.Get_List_1();

            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                //this.count = 0;
            }
            else
            {
                this.cboDonvi.Properties.DataSource = ds.Tables[0];
            }
        }

        private void Load_NV()
        {
            DataSet ds = new DataSet("ds");
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            dv.madonvi = this.cboDonvi.EditValue.ToString().Trim();
            ds = dv.GetListNV(dv.madonvi);

            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                //this.count = 0;
            }
            else
            {
                this.cboNhanvien.Properties.DataSource = ds.Tables[0];
            }
        }

        private void cboDonvi_EditValueChanged(object sender, EventArgs e)
        {
            this.Load_NV();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                this.hoadon.ngaygiaoviec = DateTime.Now;
                this.hoadon.nguoigiaoviec = this.user.id_nv;
                this.hoadon.nguoinhanviec = int.Parse(this.cboNhanvien.EditValue.ToString().Trim());
                this.hoadon.ngayhoanthanh = Convert.ToDateTime(this.txtNgayhoanthanh.EditValue.ToString().Trim());
                this.hoadon.ghichu = this.txtGhichu.Text;
                this.hoadon.trangthai = 2;
            }
            catch
            {
                XtraMessageBox.Show("Bạn phải chọn nhân viên thực hiện May!", "Thông báo");
                return;
            }
            if(this.hoadon.ExcuteProcedure(2) > 0)
            {
                for (int i = 0; i < this.count; i++)
                {
                    //if (Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim()) > 0)
                    //{
                        cs_erp_hoadon_nguyenlieuInfo ct = new cs_erp_hoadon_nguyenlieuInfo();
                        ct.id_hoadon = this.hoadon.id_hd;
                        ct.id_nguyenlieu = Convert.ToInt32(this.gridView1.GetRowCellValue(i, grdcID).ToString().Trim());
                        ct.soluong = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcSoluong).ToString().Trim());
                        ct.dongia = Convert.ToDouble(this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim());
                        if (ct.ExcuteProcedure(1) <= 0)
                        {
                            ct.ExcuteProcedure(2);
                        }
                    //}
                }
                XtraMessageBox.Show("Lưu thành công!", "Thông báo");
                //In so do
                cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
                kh.id_kh = this.hoadon.id_kh;
                kh.Get_Info();
                this.inSodo(kh);
                this.Close();
            }
        }

        private void inSodo(cs_erp_khachhangInfo kh)
        {
            DataSet ds = new DataSet("ds");
            cs_erp_khachhang_sodoInfo sd = new cs_erp_khachhang_sodoInfo();
            sd.id_khachhang = kh.id_kh;
            ds = sd.Get_List_Insodo(this.hoadon.id_hd);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("Bạn chưa nhập số đo cho khách hàng: " + kh.tenkhachhang, "Thông báo!");
                return;
            }
            cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
            dv.captren = "0";
            dv.Get_Info_by_Parents();
            rptSodo rpt = new rptSodo();
            rpt.DataSource = ds.Tables[0];
            rpt.dsHoadon = ds.Tables[0];
            //rpt.dsHoadon = this.hoadon.Get_HD_Tinhtien().Tables[0];
            rpt.lbDonvi.Text = dv.tendonvi;
            rpt.lbDiachi.Text = dv.diachi;
            rpt.lblDienthoai.Text = dv.sodt_fax;
            rpt.lblHoten.Text = kh.tenkhachhang;
            rpt.lblDiachi.Text = kh.diachi + " - Điện thoại: " + kh.dienthoai;
            rpt.InBaoCao(ds.Tables[0]);
            rpt.ShowPreview();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose(true);
        }
    }
}